package processor;

import java.util.Calendar;
import java.util.GregorianCalendar;
import java.util.HashMap;
import java.util.Set;

import bean.PatientGeneralInfo;
import bean.labTests.BloodPressure;
import bean.labTests.GlucoseTest;
import bean.labTests.LipidProfile;

public class PatientRecordProcessor {
	
	private PatientGeneralInfo patientGeneralInfo;
	
	private HashMap<String, Object> mapParameters = new HashMap<String, Object>();

	public PatientRecordProcessor(PatientGeneralInfo generalInfo){
		
		patientGeneralInfo = generalInfo;
		mapParameters.put("Sex", patientGeneralInfo.getSex())	;
		mapParameters.put("Age", this.getPatientAge());
		mapParameters.put("BMI", patientGeneralInfo.getPersonalInfo()!=null?patientGeneralInfo.getPersonalInfo().getBmi():0);
		mapParameters.put("PhysicalActivity",patientGeneralInfo.getPersonalInfo()!=null?patientGeneralInfo.getPersonalInfo().isExerciseRegularly():null);
		mapParameters.put("Smoking",patientGeneralInfo.getPersonalInfo()!=null?patientGeneralInfo.getPersonalInfo().isSmoking():null);
		mapParameters.put("Alcohol",patientGeneralInfo.getPersonalInfo()!=null?patientGeneralInfo.getPersonalInfo().isAlcohol():null);
		mapParameters.put("Ethnicity",patientGeneralInfo.getPersonalInfo()!=null?patientGeneralInfo.getEthnicity():null);
		
		
		mapParameters.put("FamilyHistoryCholesterol", patientGeneralInfo.getFamilyHistory() != null?patientGeneralInfo.getFamilyHistory().isHighCholesterol():null);
		mapParameters.put("FamilyHistoryDiabetes", patientGeneralInfo.getFamilyHistory() != null?patientGeneralInfo.getFamilyHistory().isDiabetes():null);
		mapParameters.put("PersonalHistoryDiabetes",patientGeneralInfo.getFamilyHistory() != null?patientGeneralInfo.getPastHistory().isDiabetes():null);
		mapParameters.put("FamilyHistoryHeartAttack",patientGeneralInfo.getFamilyHistory() != null?patientGeneralInfo.getFamilyHistory().isPrematuredHeartAttack():null);
		
		mapParameters.put("HDL", this.getAverageLipid("HDL"));
		mapParameters.put("LDL",this.getAverageLipid("LDL"));
		mapParameters.put("Triglycerides", this.getAverageLipid("Triglycerides"));
		mapParameters.put("Cholesterol",this.getAverageLipid("Cholesterol"));
		
		mapParameters.put("Systolic",this.getAverageSystolic());
		mapParameters.put("Diastolic",this.getAverageDiastolic());
		
		mapParameters.put("FastingGlucoseLevel",this.getAverageFastingGlucose());
		mapParameters.put("OGTT",this.getAverageOGTT());
		
				
	}
	
	public Object getRequestedPatientRecord(String requestedParam){
		return mapParameters.get(requestedParam);
	}
	
	
	private double getAverageSystolic(){
		Set<BloodPressure> bloodPressureReadings = patientGeneralInfo.getBloodPressureReadings();
		
		long sum = 0;
		if(bloodPressureReadings.size() == 0){
			return 0;
		}else{
			for(BloodPressure bp : bloodPressureReadings){
				sum += bp.getSystolic();
			}
			return sum/bloodPressureReadings.size();
		}
	}
	
	private double getAverageDiastolic(){
		Set<BloodPressure> bloodPressureReadings = patientGeneralInfo.getBloodPressureReadings();
		long sum = 0;
		if(bloodPressureReadings.size() == 0){
			return 0;
		}else{
			for(BloodPressure bp : bloodPressureReadings){
				sum += bp.getDiastolic();
			}
			return sum/bloodPressureReadings.size();
		}
	}
	
	
	private double getAverageFastingGlucose(){
		Set<GlucoseTest> glucoseTests = patientGeneralInfo.getGlucoseTests();
		long sum = 0;
		if(glucoseTests.size() == 0){
			return 0;
		}else{
			for(GlucoseTest gt : glucoseTests){
				sum += gt.getFastingGlucose();
			}
			return sum/glucoseTests.size();
		}
	}
	
	private double getAverageOGTT(){
		Set<GlucoseTest> glucoseTests = patientGeneralInfo.getGlucoseTests();
		long sum = 0;
		if(glucoseTests.size() == 0){
			return 0;
		}else{
			for(GlucoseTest gt : glucoseTests){
				sum += gt.getOgtt();
			}
			return sum/glucoseTests.size();
		}
	}
	
	private double getAverageLipid(String param){
		Set<LipidProfile> lipidTests = patientGeneralInfo.getLipidTests();
		long sum = 0;
		if(lipidTests.size() == 0){
			return 0;
		}else{
			for(LipidProfile gt : lipidTests){
				if(param.equalsIgnoreCase("HDL")){
					sum+=gt.getHdl();
				}else if(param.equalsIgnoreCase("LDL")){
					sum+=gt.getLdl();
				}else if(param.equalsIgnoreCase("Triglycerides")){
					sum+=gt.getTriglycerides();
				}else{
					sum+=gt.getTotalCholesterol();
				}
			}
			return sum/lipidTests.size();
		}
	}
	
	
	private int getPatientAge(){
		
		// Create a calendar object with the date of birth		
		Calendar dateOfBirth = Calendar.getInstance();
		dateOfBirth.setTime(patientGeneralInfo.getDateOfBirth());
	

		// Create a calendar object with today's date
		Calendar today = Calendar.getInstance();

		// Get age based on year
		int age = today.get(Calendar.YEAR) - dateOfBirth.get(Calendar.YEAR);

		// Add the tentative age to the date of birth to get this year's birthday
		dateOfBirth.add(Calendar.YEAR, age);

		// If this year's birthday has not happened yet, subtract one from age
		if (today.before(dateOfBirth)) {
		    age--;
		}
		return age;
		
	}
}
